package com.zy.servlet.user;

import com.google.gson.Gson;
import com.zy.entity.User;
import com.zy.service.FindUserByNameService;
import com.zy.service.impl.FindUserByNameServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/findUserByName")
public class FindUserByNameServlet extends HttpServlet {
    private FindUserByNameService findUserByNameService = new FindUserByNameServiceImpl();
    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String name = request.getParameter("name");
        response.setContentType("application/json;charset=UTF-8");
        try {
            List<User> users = findUserByNameService.findUserByName(name);
            response.getWriter().write(gson.toJson(users));
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write(gson.toJson("发生错误：" + e.getMessage()));
        }
    }
} 